package mx.netbrain.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import mx.netbrain.dao.RepositoriesDao;
import mx.netbrain.dao.RepositoryGroupDao;
import mx.netbrain.dao.impl.RepositoriesDaoImpl;
import mx.netbrain.dao.impl.RepositoryGroupDaoImpl;
import mx.netbrain.model.Groups;
import mx.netbrain.model.Organization;
import mx.netbrain.model.Repositories;
import mx.netbrain.model.RepositoriesGroups;
import mx.netbrain.model.RepositoriesGroupsId;
import mx.netbrain.model.Status;
import mx.netbrain.model.Storage;
import mx.netbrain.util.CreateFolderUtil;

public class RepositoriesController {

	private static char sep = File.separatorChar;

	public static Repositories saveRepository(String name, String description,
			Organization organization, Storage storage) {
		RepositoriesDao repositoriesDao = new RepositoriesDaoImpl();
		Repositories repository = new Repositories();
		Status status = new Status();
		repository.setName(name);
		repository.setDescription(description);
		repository.setOrganization(organization);
		repository.setRegistro(new Date());
		status.setId(1);
		repository.setStatus(status);
		repository.setStorage(storage);
		repositoriesDao.save(repository);
		CreateFolderUtil.createFolderUtil(repository.getStorage().getRoute()
				+ "3" + sep + repository.getId());
		TreeController.saveTree(repository);
		return repository;
	}

	public static void updateRepository(Repositories repository) {
		RepositoriesDao repositoriesDao = new RepositoriesDaoImpl();
		repositoriesDao.update(repository);
	}

	public static void deleteRepository(Groups groups, Repositories repository) {
		RepositoryGroupDao repositoryGroupDao = new RepositoryGroupDaoImpl();
		repositoryGroupDao.delete(groups, repository);
	}

	public static boolean validateRepository(List<Long> listUserGroup,
			Repositories repository) {
		List<Groups> listGroupsRepository = new ArrayList<Groups>();
		RepositoryGroupDao repositoryGroupDao = new RepositoryGroupDaoImpl();
		listGroupsRepository = repositoryGroupDao
				.findGroupForRepository(repository);
		List<Long> listUserGroup1 = new ArrayList<Long>();
		for (Groups groupsFor : listGroupsRepository) {
			listUserGroup1 = GroupsController.listUserForGroup(groupsFor);
			for (int i = 0; i < listUserGroup1.size(); i++) {
				for (int j = 0; j < listUserGroup.size(); j++) {
					if (listUserGroup1.get(i).equals(listUserGroup.get(j))) {
						return true;
					}
				}
			}
		}
		return false;
	}

	public static void saveRepositoriesGroups(Groups groups,
			Repositories repository) {
		RepositoryGroupDao repositoryGroupDao = new RepositoryGroupDaoImpl();
		RepositoriesGroups repositoriesgroup = new RepositoriesGroups();
		RepositoriesGroupsId repositoriesgroupid = new RepositoriesGroupsId();
		repositoriesgroupid.setGroups(groups.getId());
		repositoriesgroupid.setRepositories(repository.getId());
		repositoriesgroup.setId(repositoriesgroupid);
		repositoryGroupDao.save(repositoriesgroup);
	}
}
